package cn.com.petrochina.lpm3.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/zj")
public class ZJController {

    private final JdbcTemplate jdbcTemplate;

    @GetMapping("/config")
    public ModelAndView config() {

        return new ModelAndView("a2zj/a2Manager");
    }

    @GetMapping("/procedureInfo")
    public JSONObject procedureInfo(
            @RequestParam(required = false) String procedureName,
            @RequestParam(required = false) Date newDate,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("跳到getProcedureInfo这边来了");

        List<Map> procedureList = new ArrayList<Map>();
        procedureList = this.getProcedureName(procedureName);
        JSONObject returnObj = new JSONObject();
        JSONArray ay = new JSONArray();
        for (Map<String, String> m : procedureList) {
            JSONObject row = new JSONObject();
            row.put("entName", m.get("PROCEDURE_NAME"));
            row.put("entType", "procedure");
            ay.add(row);
        }

        returnObj.put("rows", ay);
        returnObj.put("total", procedureList.size());
        return returnObj;
    }

    @GetMapping("/buildQualityTree")
    public JSONObject buildQualityTree(HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
//        LPMLoginUser user = (LPMLoginUser) request.getSession().getAttribute("loginUser");
//        JSONObject root = new JSONObject();
//        root.put("id", 0);
//        root.put("text", "质检项目目录");
//        root.put("state", "open");
//
//        JSONObject attributes=new JSONObject();
//        attributes.put("isParent", true);
//        root.put("attributes", attributes);
//        JSONArray ay=zjQualityModelManage.queryQualityProjectTree(user.getOrgLevel());
//        response.setContentType("application/text;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        out.write(ay.toJSONString());
//        out.flush();
//        out.close();
        return null;
    }

    private List getProcedureName(String procedureName) {
        String sql = "SELECT distinct name AS PROCEDURE_NAME FROM USER_SOURCE  where type='PROCEDURE' and name like '%DQC%'";
        if (procedureName != null && !"".equals(procedureName) && procedureName.length() > 0) {
            sql = sql + " and name like'%" + procedureName + "%'";
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }
}
